use eframe::egui::{self, vec2, Button};
use image::io::Reader as ImageReader;
use std::io::Cursor;

pub struct ToolMenu {}

impl Default for ToolMenu {
    fn default() -> Self {
        ToolMenu::new()
    }
}

impl ToolMenu {
    pub fn new() -> Self {
        Self {}
    }

    pub fn show(&mut self, ui: &mut egui::Ui) {
        ui.horizontal(|ui| {
            let button_size = vec2(30.0, 30.0); // 设置图像按钮的大小

            // Group 1
            let mouse_icon =
                egui::Image::new(egui::include_image!("../assets/tool_menu/mouse.svg"));
            if ui
                .add_sized(button_size, egui::ImageButton::new(mouse_icon))
                .clicked()
            {
                println!("Image button clicked!");
            }
            let cross_hairs_icon =
                egui::Image::new(egui::include_image!("../assets/tool_menu/十字标.svg"));
            if ui
                .add_sized(button_size, egui::ImageButton::new(cross_hairs_icon))
                .clicked()
            {
                println!(" 十字标 button clicked!");
            }

            ui.separator();

            // Group 2
            let vertical_line_icon =
                egui::Image::new(egui::include_image!("../assets/tool_menu/竖线.svg"));
            if ui
                .add_sized(button_size, egui::ImageButton::new(vertical_line_icon))
                .clicked()
            {
                println!("Image button clicked!");
            }
            let horizontal_line_icon =
                egui::Image::new(egui::include_image!("../assets/tool_menu/水平线.svg"));
            if ui
                .add_sized(button_size, egui::ImageButton::new(horizontal_line_icon))
                .clicked()
            {
                println!("Image button clicked!");
            }
            let segment_line_icon =
                egui::Image::new(egui::include_image!("../assets/tool_menu/线段.svg"));
            if ui
                .add_sized(button_size, egui::ImageButton::new(segment_line_icon))
                .clicked()
            {
                println!("Image button clicked!");
            }
            let parallel_line_icon =
                egui::Image::new(egui::include_image!("../assets/tool_menu/平行线.svg"));
            if ui
                .add_sized(button_size, egui::ImageButton::new(parallel_line_icon))
                .clicked()
            {
                println!("Image button clicked!");
            }

            let fibonacci_line_icon =
                egui::Image::new(egui::include_image!("../assets/tool_menu/斐波那契回撤.svg"));
            if ui
                .add_sized(button_size, egui::ImageButton::new(fibonacci_line_icon))
                .clicked()
            {
                println!("Image button clicked!");
            }

            ui.separator();

            // Group 3
            if ui.add_sized(button_size, Button::new("M1")).clicked() {
                // Handle "M1" action
            }
            if ui.add_sized(button_size, Button::new("M5")).clicked() {
                // Handle "M5" action
            }
            if ui.add_sized(button_size, Button::new("M15")).clicked() {
                // Handle "M15" action
            }
            if ui.add_sized(button_size, Button::new("M30")).clicked() {
                // Handle "M30" action
            }
            if ui.add_sized(button_size, Button::new("H1")).clicked() {
                // Handle "H1" action
            }
            if ui.add_sized(button_size, Button::new("H4")).clicked() {
                // Handle "H4" action
            }
            if ui.add_sized(button_size, Button::new("D1")).clicked() {
                // Handle "D1" action
            }
            if ui.add_sized(button_size, Button::new("W1")).clicked() {
                // Handle "W1" action
            }
            if ui.add_sized(button_size, Button::new("MN")).clicked() {
                // Handle "MN" action
            }
            ui.separator();

            // Group 4

            {
                let chart_icon =
                    egui::Image::new(egui::include_image!("../assets/tool_menu/图表.svg"));
                if ui
                    .add_sized(button_size, egui::ImageButton::new(chart_icon))
                    .clicked()
                {
                    println!("Image button clicked!");
                }
                let polyline_icon =
                    egui::Image::new(egui::include_image!("../assets/tool_menu/折线图_area.svg"));
                if ui
                    .add_sized(button_size, egui::ImageButton::new(polyline_icon))
                    .clicked()
                {
                    println!(" polyline_icon Image button clicked!");
                }
                let order_icon =
                    egui::Image::new(egui::include_image!("../assets/tool_menu/报价.svg"));
                if ui
                    .add_sized(button_size, egui::ImageButton::new(order_icon))
                    .clicked()
                {
                    println!("Image button clicked!");
                }
            }

            ui.separator();

            // Group 5
            {
                if ui.add_sized(button_size, Button::new("IDE")).clicked() {
                    // Handle "MN" action
                }
                let market_icon =
                    egui::Image::new(egui::include_image!("../assets/tool_menu/市场.svg"));
                if ui
                    .add_sized(button_size, egui::ImageButton::new(market_icon))
                    .clicked()
                {
                    println!("Image button clicked!");
                }
                let vps_icon =
                    egui::Image::new(egui::include_image!("../assets/tool_menu/云服务器.svg"));
                if ui
                    .add_sized(button_size, egui::ImageButton::new(vps_icon))
                    .clicked()
                {
                    println!("Image button clicked!");
                }
            }

            ui.separator();

            // Group 6
            {
                let ea_icon =
                    egui::Image::new(egui::include_image!("../assets/tool_menu/全程自动交易.svg"));
                if ui
                    .add_sized(button_size, egui::ImageButton::new(ea_icon))
                    .clicked()
                {
                    println!("Image button clicked!");
                }
                let neworder_icon =
                    egui::Image::new(egui::include_image!("../assets/tool_menu/订单.svg"));
                if ui
                    .add_sized(button_size, egui::ImageButton::new(neworder_icon))
                    .clicked()
                {
                    println!("Image button clicked!");
                }
            }

            ui.separator();

            // Group 7
            {
                let candlestick_icon =
                    egui::Image::new(egui::include_image!("../assets/tool_menu/蜡烛图.svg"));
                if ui
                    .add_sized(button_size, egui::ImageButton::new(candlestick_icon))
                    .clicked()
                {
                    println!("Image button clicked!");
                }
                let polyline_icon =
                    egui::Image::new(egui::include_image!("../assets/tool_menu/折线图.svg"));
                if ui
                    .add_sized(button_size, egui::ImageButton::new(polyline_icon))
                    .clicked()
                {
                    println!("Image button clicked!");
                }
            }

            ui.separator();
            // Group 8
            {
                let zoomout_icon =
                    egui::Image::new(egui::include_image!("../assets/tool_menu/放大.svg"));
                if ui.add(egui::ImageButton::new(zoomout_icon)).clicked() {
                    println!("Image button clicked!");
                }
                // 设定按钮的大小

                let zoomin_icon =
                    egui::Image::new(egui::include_image!("../assets/tool_menu/缩小.svg"));
                if ui
                    .add_sized(button_size, egui::ImageButton::new(zoomin_icon))
                    .clicked()
                {
                    println!("Image button clicked!");
                }
                let layout_icon =
                    egui::Image::new(egui::include_image!("../assets/tool_menu/布局.svg"));
                if ui
                    .add_sized(button_size, egui::ImageButton::new(layout_icon))
                    .clicked()
                {
                    println!("Image button clicked!");
                }
            }

            ui.separator();

            // Group 9
            {
                let placement_right_icon =
                    egui::Image::new(egui::include_image!("../assets/tool_menu/置右.svg"));
                if ui
                    .add_sized(button_size, egui::ImageButton::new(placement_right_icon))
                    .clicked()
                {
                    println!("Image button clicked!");
                }
            }
        });
    }
}
